package cn.tedu.eb_film.service.impl;

import cn.tedu.eb_film.ex.ServiceException;
import cn.tedu.eb_film.mapper.InformationMapper;
import cn.tedu.eb_film.pojo.dto.InformationAddNewDTO;
import cn.tedu.eb_film.pojo.dto.InformationUpdateDTO;
import cn.tedu.eb_film.pojo.entity.CsParkingRate;
import cn.tedu.eb_film.pojo.entity.Information;
import cn.tedu.eb_film.pojo.vo.CsParkingRateStandardVO;
import cn.tedu.eb_film.pojo.vo.InformationListItemVO;
import cn.tedu.eb_film.pojo.vo.InformationStandardVO;
import cn.tedu.eb_film.pojo.vo.UserListItemVO;
import cn.tedu.eb_film.service.InformationService;
import cn.tedu.eb_film.utils.ServiceCode;
import cn.tedu.eb_film.utils.StateCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * 资讯表(Information)表服务实现类
 *
 * @author makejava
 * @since 2023-03-18 10:46:06
 */
@Service("informationService")
@Slf4j
public class InformationServiceImpl implements InformationService {
    @Autowired
    private InformationMapper informationMapper;


    /**
     * 新增数据
     *
     * @param informationAddNewDTO 实例对象
     * @return 实例对象
     */
    @Override
    public void insert(InformationAddNewDTO informationAddNewDTO) {
        log.debug("开始处理添加咨询，参数为：{}",informationAddNewDTO);
        int row = informationMapper.countByConnect(informationAddNewDTO.getContent());
        if (row>0){
            String message = "添加失败，内容不能重复！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE,message);
        }
        row = informationMapper.countByKeywords(informationAddNewDTO.getKeywords());
        if (row>0){
            String message = "添加失败，咨询关键词已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE,message);
        }
        row = informationMapper.countByImgsUrl(informationAddNewDTO.getImgsUrl());
        if (row>0){
            String message = "添加失败，咨询图片链接已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE,message);
        }

        Information information = new Information();
        BeanUtils.copyProperties(informationAddNewDTO, information);
        log.debug("即将插入咨询则，参数为：{}",information);
        row = informationMapper.insert(information);
        if (row!=1){
            String message = "添加失败，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN,message);
        }
        log.debug("插入咨询成功，参数为：{},",information);
    }


    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public void deleteById(Long id) {

        log.debug("即将执行删除操作，id为：{}",id);
        InformationStandardVO informationStandardVO = informationMapper.selectById(id);
        log.debug("通过id查询数据，参数为：{}",informationStandardVO);
        if (informationStandardVO==null){
            String message = "删除失败，数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        int row = informationMapper.deleteById(id, StateCode.STATE_DELETE.getValue());
        if (row!=1){
            String message = "删除失败，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN,message);
        }
        InformationStandardVO informationStandardVO1 = informationMapper.selectById(id);
        log.debug("删除成功，数据为：{}",informationStandardVO1);

    }


    /**
     * 根绝ID修改数据
     *
     * @param informationUpdateDTO 实例对象
     * @return 实例对象
     */
    @Override
    public void updateById(Long id, InformationUpdateDTO informationUpdateDTO) {
        log.debug("开始执行修改业务,id为：{}，参数为：{}",id,informationUpdateDTO);
        InformationStandardVO informationStandardVO = informationMapper.selectById(id);
        log.debug("根据id查询停车规则,参数为：{}",informationStandardVO);
        if (informationStandardVO==null){
            String message = "修改停车规则失败，停车规则不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        String contect = informationUpdateDTO.getContent();
        int rows = informationMapper.countByConnect(contect);
        log.debug("根据咨询内容【{}【统计数量，结果为：{}",contect,rows);
        if (rows>0){
            String message = "修改咨询内容失败，咨询内容已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE,message);
        }
        rows = informationMapper.countByKeywords(informationUpdateDTO.getKeywords());
        if (rows>0){
            String message = "修改咨询关键词失败，咨询关键词已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE,message);
        }
        rows = informationMapper.countByImgsUrl(informationUpdateDTO.getImgsUrl());
        if (rows>0){
            String message = "修改咨询图片链接失败，咨询图片链接已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE,message);
        }
        String keywords = informationUpdateDTO.getKeywords();
        rows = informationMapper.countByKeywords(keywords);
        log.debug("根据咨询内容【{}【统计数量，结果为：{}",keywords,rows);
        if (rows>0){
            String message = "修改咨询关键词失败，咨询关键词已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE,message);
        }

        String imgsUrl = informationUpdateDTO.getImgsUrl();
        rows = informationMapper.countByImgsUrl(keywords);
        log.debug("根据咨询图片链接【{}【统计数量，结果为：{}",imgsUrl,rows);
        if (rows>0){
            String message = "修改咨询图片链接失败，咨询图片链接已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DATA_PRESENCE,message);
        }

        Information information = new Information();
        BeanUtils.copyProperties(informationUpdateDTO,information);
        information.setId(id);
        log.debug("即将修改停车规则数据，参数为：{}",information);
        rows = informationMapper.updateById(information);
        if (rows!=1){
            String message = "修改停车规则失败，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN,message);
        }
        log.debug("修改数据成功，修改后参数为：{}",information);

    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public InformationStandardVO selectById(Long id) {
        InformationStandardVO informationStandardVO = informationMapper.selectById(id);
        if (informationStandardVO==null || informationStandardVO.getState().equals(StateCode.STATE_DELETE.getValue())){
            String message = "查询咨询失败，数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return informationStandardVO;
    }

    /**
     * 查询数据列表
     *
     * @return 实例对象列表
     */
    @Override
    public List<InformationListItemVO> selectList() {
        List<InformationListItemVO> list = informationMapper.selectList();
        list.removeIf(information->information.getState().equals(StateCode.STATE_DELETE.getValue()));
        return list;
    }
}
